package com.freedom.education.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freedom.base.common.Result;
import com.freedom.education.pojo.Lecturer;
import com.freedom.education.query.LecturerQuery;
import com.freedom.education.service.LecturerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Description:
 * <p>CreateTime: 2023-03-11 上午 2:35</p>
 * <p>Email: 2396598264@qq.com</p>
 *
 * @author Chb
 */

@Api(tags = "讲师服务接口")
@RestController
@RequestMapping("service-education/lecturer")
public class LecturerController {

    @Autowired
    private LecturerService lecturerService;

    @ApiOperation("分页 + 条件查询讲师")
    @GetMapping
    public Result<Page<Lecturer>> pageByCondition(LecturerQuery lecturerQuery) {
        Page<Lecturer> lecturerList = lecturerService.pageByCondition(lecturerQuery);
        return Result.succeed(lecturerList);
    }

    @ApiOperation("根据ID查询讲师")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "讲师ID", required = true, paramType = "path", dataTypeClass = Long.class)
    })
    @GetMapping("/{id}")
    public Result<Lecturer> getById(@PathVariable("id") Long id) {
        Lecturer lecturer = lecturerService.getById(id);
        return lecturer != null ? Result.succeed(lecturer) : Result.fail("该讲师不存在");
    }

    @ApiOperation("添加讲师")
    @PostMapping
    public Result<Object> save(@RequestBody Lecturer lecturer) {
        lecturerService.save(lecturer);
        return Result.succeed(null);
    }

    @ApiOperation("根据ID删除讲师")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "讲师ID", required = true, paramType = "path", dataTypeClass = Long.class)
    })
    @DeleteMapping("/{id}")
    public Result<Object> removeById(@PathVariable("id") Long id) {
        lecturerService.removeById(id);
        return Result.succeed(null);
    }

    @ApiOperation("修改讲师")
    @PutMapping
    public Result<Object> update(@RequestBody Lecturer lecturer) {
        lecturerService.updateById(lecturer);
        return Result.succeed(null);
    }
}
